package com.dada.tiku.service.impl;

import java.text.DateFormat;
import java.text.FieldPosition;
import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.dada.tiku.dao.mapper.TUserMapper;
import com.dada.tiku.pojo.TUser;
import com.dada.tiku.pojo.TUserExample;
import com.dada.tiku.pojo.TUserExample.Criteria;
import com.dada.tiku.service.UserService;
import com.dada.tiku.utils.WxResult;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	private TUserMapper userMapper;
	
	public TUser findUserByopenid(String openid) {
		TUserExample example = new TUserExample();
		Criteria criteria = example.createCriteria();
		criteria.andOpenidEqualTo(openid);
		List<TUser> list = userMapper.selectByExample(example);
		if(list == null || list.isEmpty() == true){			
			return null;
		}
		return list.get(0);
	}

	
	public WxResult createUser(TUser userinfo) {
		userinfo.setPassword("123456789");
		SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
		String format = s.format(new Date());
		userinfo.setCreatetime(format);
		userinfo.setName(userinfo.getNickname());
		userinfo.setStatus("1");
		int row = userMapper.insert(userinfo);
		if(row != 1){
			return WxResult.build(500, "用户创建失败");
		}
		return WxResult.ok();
	}


	
}
